//
//  LKChooseBankCardListViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/6/6.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKChooseBankCardListViewController.h"
#import "LKWorkOrderListDetailCell.h"
#import "LKWorkOrderListDetailPictureCell.h"
#import "LKOrderEditTopView.h"
@interface LKChooseBankCardListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)LKBaseTableview *tableview;
@property(nonatomic,strong)NSMutableArray *banklistArray;
@property(nonatomic,strong)NSMutableArray *idsArray;
@property(nonatomic,strong)NSMutableArray *photosurlArray;
@property(nonatomic,strong)NSMutableArray *isAutenticArray;
@end

@implementation LKChooseBankCardListViewController
-(LKBaseTableview *)tableview{
    if (!_tableview) {
        _tableview = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT + 12, WIDTH , HEIGHT - NAVIGATION_BAR_HEIGHT - 12) style:UITableViewStyleGrouped registercellClasss:@[@"LKWorkOrderListDetailCell",@"LKWorkOrderListDetailPictureCell"]];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.emptyDataSetSource = self;
        _tableview.emptyDataSetDelegate = self;
    }
    return _tableview;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableview];
    [self getBankListData];
    // Do any additional setup after loading the view.
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
    self.navigationItem.title = @"银行卡列表";
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.banklistArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *arr = self.banklistArray[section];
    return arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.addOrderModel.IdentitfyCategory isEqual:@"1"]) {
        LKWorkOrderListDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkOrderListDetailCell"];
        cell.confirmImageView.hidden = YES;
        cell.titileLabel.text = self.banklistArray[indexPath.section][indexPath.row];
        cell.arrowBtn.hidden = YES;
        cell.lineView.hidden = YES;
        NSArray *arr = self.banklistArray[indexPath.section];
        
        if (indexPath.row == 0) {
            cell.confirmImageView.hidden = NO;
            if ([self.isAutenticArray[indexPath.section] boolValue]) {
                cell.confirmImageView.image = [UIImage imageNamed:@"confirmBankCard"];
//                return cell;
            }else{
                cell.confirmImageView.hidden = NO;
                cell.confirmImageView.image = [UIImage imageNamed:@"unconfirmBankCard"];
//                return cell;
            }
        }
        if (indexPath.section == 0) {
            if (indexPath.row == arr.count - 1) {
//                LKWorkOrderListDetailPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkOrderListDetailPictureCell"];
//                [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,self.photosurlArray[indexPath.section]]] placeholderImage:[UIImage imageNamed:@"pic"]];
                cell.titileLabel.text = self.banklistArray[indexPath.section][indexPath.row];
                return cell;
            }
        }else{
            if (indexPath.row == arr.count - 1) {
//                LKWorkOrderListDetailPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkOrderListDetailPictureCell"];
//                [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,self.photosurlArray[indexPath.section]]] placeholderImage:[UIImage imageNamed:@"pic"]];
                cell.titileLabel.text = self.banklistArray[indexPath.section][indexPath.row];
                return cell;
            }
            
        }
        return cell;
    }else{
        LKWorkOrderListDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkOrderListDetailCell"];
        
        cell.titileLabel.text = self.banklistArray[indexPath.section][indexPath.row];
        cell.arrowBtn.hidden = YES;
        cell.lineView.hidden = YES;
        cell.confirmImageView.hidden = YES;
        NSArray *arr = self.banklistArray[indexPath.section];
        if (indexPath.row == 0) {
            cell.confirmImageView.hidden = NO;
            if ([self.isAutenticArray[indexPath.section] boolValue]) {
                cell.confirmImageView.image = [UIImage imageNamed:@"confirmBankCard"];
                return cell;
            }else{
                cell.confirmImageView.hidden = NO;
                cell.confirmImageView.image = [UIImage imageNamed:@"unconfirmBankCard"];
                return cell;
            }
        }
        if (indexPath.section == 0) {
            if (indexPath.row == arr.count - 1) {
                LKWorkOrderListDetailPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkOrderListDetailPictureCell"];
                [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,self.photosurlArray[indexPath.section]]] placeholderImage:[UIImage imageNamed:@"pic"]];
                cell.titleLabel.text = self.banklistArray[indexPath.section][indexPath.row];
                return cell;
            }
        }else{
            if (indexPath.row == arr.count - 1) {
                LKWorkOrderListDetailPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkOrderListDetailPictureCell"];
                [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,self.photosurlArray[indexPath.section]]] placeholderImage:[UIImage imageNamed:@"pic"]];
                cell.titleLabel.text = self.banklistArray[indexPath.section][indexPath.row];
                return cell;
            }
            
        }
        return cell;
    }

}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.00000000001;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 12;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.addOrderModel.IdentitfyCategory isEqual:@"1"]) {
//        NSArray *arr = self.banklistArray[indexPath.section];
//        if (indexPath.row == arr.count - 1) {
//            return 90;
//        }
        return 50;
    }else{
        NSArray *arr = self.banklistArray[indexPath.section];
        if (indexPath.row == arr.count - 1) {
            return 90;
        }
        return 50;
    }

}
/**
 获取银行卡列表信息
 */
-(void)getBankListData{
    NSDictionary *param;
    if (self.isOrderUpdate) {
        param = @{@"CustomerCode":self.addOrderModel.CustomerCode};
    }else{
        param = @{@"CustomerCode":self.customermodel.customerCode};
    }
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:bankListUrl header:[LKUserInforTool header] parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        
        NSArray *bankArray = responseObject[@"result"];
        for (NSDictionary *dict in bankArray) {
            NSString *bankCode = [NSString stringWithFormat:@"%@%@",@"账户卡号：",dict[@"accountCode"]];
            NSString *bankName = [NSString stringWithFormat:@"%@%@",@"开户行：",dict[@"bankName"]];
            NSString *BankBranch = [NSString stringWithFormat:@"%@%@",@"开户行支行：",dict[@"bankBranch"]];
            
            NSString *UsrBankName = [NSString stringWithFormat:@"%@%@",@"户名：",dict[@"accountName"]];
            NSString *bankphotoUrl = [NSString stringWithFormat:@"%@",@"银行卡正面复印件："];
            NSString *bankCity = [NSString stringWithFormat:@"%@%@",@"开户行支行省市：",dict[@"bankCity"]];
            NSString *Id = [NSString stringWithFormat:@"%@",dict[@"id"]];
            NSArray *bankArra;
            NSString *bankCardTel = [NSString stringWithFormat:@"银行卡预留手机号：%@",dict[@"bankCardTel"]];
            if ([self.addOrderModel.IdentitfyCategory isEqual:@"1"]) {
                    bankArra = @[bankCode,UsrBankName,bankName,BankBranch,bankCity,bankCardTel];
            }else{
                    bankArra = @[bankCode,UsrBankName,bankName,BankBranch,bankCity,bankphotoUrl];
            }
            [self.isAutenticArray addObject:dict[@"isAuthenticated"]];
            [self.photosurlArray addObject:dict[@"accountPhotoUrl"]];
            [self.banklistArray addObject:bankArra];
            [self.idsArray addObject:Id];
        }
        [self.tableview reloadData];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *arra = self.banklistArray[indexPath.section];
    if (self.selectSection == 0) {
        //划扣银行卡
        self.addOrderModel.PayAccountCode = [[arra[0] componentsSeparatedByString:@"："] lastObject];
        self.addOrderModel.PayBankName = [[arra[2] componentsSeparatedByString:@"："] lastObject];
        self.addOrderModel.PayBankBranch = [[arra[3] componentsSeparatedByString:@"："] lastObject];
        self.addOrderModel.PayAccountName = [[arra[1] componentsSeparatedByString:@"："] lastObject];
        self.addOrderModel.PayBankCity = [[arra[4] componentsSeparatedByString:@"："] lastObject];
        self.addOrderModel.PayAccountPhotoUrl = self.photosurlArray[indexPath.section];
        self.addOrderModel.PayBankCardId = self.idsArray[indexPath.section];
        //回款银行卡
        self.addOrderModel.RepayAccountCode = [[arra[0] componentsSeparatedByString:@"："] lastObject];
        self.addOrderModel.RepayBankName = [[arra[2] componentsSeparatedByString:@"："] lastObject];;
        self.addOrderModel.RepayBankBranch = [[arra[3] componentsSeparatedByString:@"："] lastObject];
        self.addOrderModel.RepayAccountName = [[arra[1] componentsSeparatedByString:@"："] lastObject];
        self.addOrderModel.RepayBankCity = [[arra[4] componentsSeparatedByString:@"："] lastObject];
        self.addOrderModel.RepayAccountPhotoUrl = self.photosurlArray[indexPath.section];
        self.addOrderModel.RepayBankCardId = self.idsArray[indexPath.section];
        self.sendArraBlock(arra,self.addOrderModel);
    }else{
        self.addOrderModel.RepayAccountCode = [[arra[0] componentsSeparatedByString:@"："] lastObject];
        self.addOrderModel.RepayBankName = [[arra[2] componentsSeparatedByString:@"："] lastObject];;
        self.addOrderModel.RepayBankBranch = [[arra[3] componentsSeparatedByString:@"："] lastObject];
        self.addOrderModel.RepayAccountName = [[arra[1] componentsSeparatedByString:@"："] lastObject];
        self.addOrderModel.RepayBankCity = [[arra[4] componentsSeparatedByString:@"："] lastObject];
        self.addOrderModel.RepayAccountPhotoUrl = self.photosurlArray[indexPath.section];
        self.addOrderModel.RepayBankCardId = self.idsArray[indexPath.section];
        self.sendArraBlock(arra,self.addOrderModel);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSMutableArray *)banklistArray{
    if (!_banklistArray) {
        _banklistArray = [[NSMutableArray alloc] init];
    }
    return _banklistArray;
}
-(NSMutableArray *)idsArray{
    if (!_idsArray) {
        _idsArray = [[NSMutableArray alloc] init];
    }
    return _idsArray;
}
-(NSMutableArray *)photosurlArray{
    if (!_photosurlArray) {
        _photosurlArray = [[NSMutableArray alloc] init];
    }
    return _photosurlArray;
}
-(NSMutableArray *)isAutenticArray{
    if (!_isAutenticArray) {
        _isAutenticArray = [[NSMutableArray alloc] init];
    }
    return _isAutenticArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
